package ithehemaTest;

public class Test1 {
    public static void main(String[] args) {
        //1.创建长度为3的数组
        Student[] arr = new Student[3];
        //2.创建学生对象
        Student s1 = new Student(1, "zhansan", 18);
        Student s2 = new Student(2, "lisi", 19);
        Student s3 = new Student(3, "wangwu", 20);
        //3.把学生对象添加在数组中
        arr[0] = s1;
        arr[1] = s2;
        arr[2] = s3;
        //4.再次创建一个学生对象
        Student s4 = new Student(4, "hema", 21);
        //先要找到id为2的学生对于的索引
        int index = getIndex(arr, 2);
        //判断索引
        if(index == -1){
            //不存在，则直接提示
            System.out.println("当前id不存在，修改失败");
        }else {
            //存在， 则将他的年龄+1岁
            int newAge=arr[index].getAge()+1;
            arr[index].setAge(newAge);
            //遍历数组
            Print(arr);
        }
    }
    public  static  void Print(Student []arr) {

        for (int i = 0; i < arr.length; i++) {
            Student stu=arr[i];
            System.out.println(stu.getId()+" "+stu.getName()+" "+stu.getAge());
        }
    }
    public  static  int getIndex(Student []arr,int id) {
        for (int i = 0; i < arr.length; i++) {
            Student stu = arr[i];
            if(arr[i]!=null){
                if(stu.getId()==id){
                    return i;
                }
            }
        }
        return -1;
    }
}
